﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ESRP
{
    public partial class NoteContabileRezumatFrm : FrmRezumat
    {
        private System.Windows.Forms.DataGridViewTextBoxColumn ContDebit;
        private System.Windows.Forms.DataGridViewTextBoxColumn ContCredit;
        private System.Windows.Forms.DataGridViewTextBoxColumn ExplicatiiDocJust;
        private System.Windows.Forms.DataGridViewTextBoxColumn valoareLeiDataGridViewTextBoxColumn;
        ESRPDataSet.BalconSetupRow BSetupRow = null;
        public String filtruNote = String.Empty;

        public int LunaCurenta = 1;

        public NoteContabileRezumatFrm(BindingSource _bs, ModuleClass mInfo)
            : base(mInfo)
        {
            if (_bs!=null)
                InitializeBindingSource(_bs);

            InitializeComponent();
            //
            if (_bs == null)
            {
                InitializeBindingSource("RulajeDoc", this.eSRPDataSet);
                listBndSourceMain.Add(rulajeDocBindingSource);
            }

            this.HandleTiparire += Tipareste;
            workConnString = mInfo.workConnectionString;
            eSRPDataSet.RulajeDoc.IDJurnalColumn.DefaultValue = 0;
            HandleSalvare += new EventHandler(Selectie);
            EnableMainBrowseButton = false;
        }

        new public void Selectie(object sender, EventArgs args)
        {
            //base.Selectie(sender, args);
            this.Close();
        }

        private void InitGrid()
        {
            
            rezNoteDGV.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn IdNrDocColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn IdDocumentColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            DataGridViewComboBoxColumn IdJurnalColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
            DataGridViewTextBoxColumn DataDocColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn ValoareLeiColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn IdNrNotaColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn TipColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            DataGridViewComboBoxColumn SurseFinantareColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
            DataGridViewComboBoxColumn SectorBugetarColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();

            IdNrDocColumn.DataPropertyName = "NumarDoc";
            IdNrDocColumn.HeaderText = "Numar Doc.";
            IdNrDocColumn.Name = "NumarDocColumn";
            IdNrDocColumn.Width = 68;
            IdNrDocColumn.ReadOnly = true;

            DataDocColumn.DataPropertyName = "DataDoc";
            DataDocColumn.HeaderText = "Data Doc.";
            DataDocColumn.Name = "DataDocColumn";
            DataDocColumn.DefaultCellStyle.Format = "dd.MM.yyyy";
            DataDocColumn.Width = 68;
            DataDocColumn.ReadOnly = true;

            ValoareLeiColumn.DataPropertyName = "ValoareLei";
            ValoareLeiColumn.HeaderText = "Val. Total";
            ValoareLeiColumn.Name = "ValoareLeiColumn";
            ValoareLeiColumn.Width = 68;
            ValoareLeiColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            ValoareLeiColumn.DefaultCellStyle.Format = "N2";
            ValoareLeiColumn.ReadOnly = true; ;

            IdDocumentColumn.DataPropertyName = "IdDocument";
            IdDocumentColumn.HeaderText = "IdDocument";
            IdDocumentColumn.Name = "IdDocumentColumn";
            IdDocumentColumn.Width = 68;
            IdDocumentColumn.Visible = false;
            IdDocumentColumn.ReadOnly = true;

            IdJurnalColumn.DataSource = this.jurnaleBindingSource;
            IdJurnalColumn.ValueMember = "IDJurnal";
            IdJurnalColumn.DisplayMember = "Denumire";
            if (jurnaleBindingSource.Count != 0)
                IdJurnalColumn.DataPropertyName = "IdJurnal";
            IdJurnalColumn.HeaderText = "Jurnal";
            IdJurnalColumn.Name = "IdJurnalColumn";
            IdJurnalColumn.Width = 108;
            IdJurnalColumn.ReadOnly = true;
            if (jurnaleBindingSource.Count == 0)
                IdJurnalColumn.Visible = false;


            SectorBugetarColumn.DataSource = this.sectorBugetarBindingSource;
            SectorBugetarColumn.ValueMember = "IDSectorBugetar";
            SectorBugetarColumn.DisplayMember = "Denumire";
            if (sectorBugetarBindingSource.Count != 0)
                SectorBugetarColumn.DataPropertyName = "IDSectorBugetar";
            SectorBugetarColumn.HeaderText = "Sector Bugetar";
            SectorBugetarColumn.Name = "SectorBugetarColumn";
            SectorBugetarColumn.Width = 108;
            SectorBugetarColumn.ReadOnly = true;
            if (sectorBugetarBindingSource.Count == 0)
                SectorBugetarColumn.Visible = false;

            SurseFinantareColumn.DataSource = this.surseFinantareBindingSource;
            SurseFinantareColumn.ValueMember = "IDSursaFinantare";
            SurseFinantareColumn.DisplayMember = "Denumire";
            if (surseFinantareBindingSource.Count != 0)
                SurseFinantareColumn.DataPropertyName = "IDSursaFinantare";
            SurseFinantareColumn.HeaderText = "Sursa Finantare";
            SurseFinantareColumn.Name = "SursaFinantareColumn";
            SurseFinantareColumn.Width = 108;
            SurseFinantareColumn.ReadOnly = true;
            if (surseFinantareBindingSource.Count == 0)
                SurseFinantareColumn.Visible = false;

            IdNrNotaColumn.DataPropertyName = "NumarNota";
            IdNrNotaColumn.HeaderText = "Numar Nota";
            IdNrNotaColumn.Name = "NumarNotaColumn";
            IdNrNotaColumn.Width = 68;
            IdNrNotaColumn.ReadOnly = true;

            TipColumn.DataPropertyName = "TipFormula";
            TipColumn.HeaderText = "Tip";
            TipColumn.Name = "TipColumn";
            TipColumn.Width = 68;
            TipColumn.ReadOnly = true;
            TipColumn.Visible = false;



            DataGridViewColumn[] coloane = new System.Windows.Forms.DataGridViewColumn[] {
                IdNrDocColumn,DataDocColumn,ValoareLeiColumn,IdDocumentColumn,IdNrNotaColumn,TipColumn,
            };

            this.rezNoteDGV.Columns.AddRange(coloane);


            if (jurnaleBindingSource.Count != 0)
            { 
                 coloane = new System.Windows.Forms.DataGridViewColumn[] {IdJurnalColumn};
                 this.rezNoteDGV.Columns.AddRange(coloane);
            }

            if (BSetupRow!=null && BSetupRow.FolosesteSectorBugetar)
            {
                coloane = new System.Windows.Forms.DataGridViewColumn[] { SectorBugetarColumn };
                this.rezNoteDGV.Columns.AddRange(coloane);
            }

            if (BSetupRow != null && BSetupRow.FolosesteSursaFinantare)
            {
                coloane = new System.Windows.Forms.DataGridViewColumn[] { SurseFinantareColumn };
                this.rezNoteDGV.Columns.AddRange(coloane);
            }

         



            rezNoteDGV.DataSource = this.listBndSourceMain[0];
            this.listBndSourceMain[0].Sort = "DataDoc Desc";


            RulajeBS.DataSource = this.listBndSourceMain[0];
            RulajeBS.DataMember = "FK_Rulaje_RulajeDoc";


            this.ContDebit = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.ContCredit = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.valoareLeiDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.ExplicatiiDocJust = new System.Windows.Forms.DataGridViewTextBoxColumn();
            // 
            // ContDebit
            // 
            this.ContDebit.DataPropertyName = "ContDebit";
            this.ContDebit.HeaderText = "Cont Debit";
            this.ContDebit.Name = "ContDebit";
            this.ContDebit.ReadOnly = true;
            // 
            // ContCredit
            // 
            this.ContCredit.DataPropertyName = "ContCredit";
            this.ContCredit.HeaderText = "Cont Credit";
            this.ContCredit.Name = "ContCredit";
            this.ContCredit.ReadOnly = true;
            // 
            // valoareLeiDataGridViewTextBoxColumn
            // 
            this.valoareLeiDataGridViewTextBoxColumn.DataPropertyName = "ValoareLei";
            this.valoareLeiDataGridViewTextBoxColumn.HeaderText = "Valoare Lei";
            this.valoareLeiDataGridViewTextBoxColumn.Name = "valoareLeiDataGridViewTextBoxColumn";
            this.valoareLeiDataGridViewTextBoxColumn.ReadOnly = true;
            valoareLeiDataGridViewTextBoxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            valoareLeiDataGridViewTextBoxColumn.DefaultCellStyle.Format = "N2";
            valoareLeiDataGridViewTextBoxColumn.Width = 80;
            // 
            // ExplicatiiDocJust
            // 
            this.ExplicatiiDocJust.DataPropertyName = "ExplicatiiDocJust";
            this.ExplicatiiDocJust.HeaderText = "Explicatii";
            this.ExplicatiiDocJust.Name = "ExplicatiiDocJust";
            this.ExplicatiiDocJust.ReadOnly = true;
            this.ExplicatiiDocJust.Width = 200;
            this.rulajeDGV.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ContDebit,
            this.ContCredit,
            this.valoareLeiDataGridViewTextBoxColumn,
            this.ExplicatiiDocJust});
        }

        public void MyGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
            /*if (!((DataGridView)sender).Focused)
                return;
            TrateazaExceptie(e.Exception);*/

        }
        private void NoteContabileFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.rulajeDocTableAdapter.Connection = con;
            this.jurnaleTableAdapter.Connection = con;
            this.rulajeTableAdapter.Connection = con;
            this.rulajeDocTableAdapter.Connection = con;
            this.balconSetupTableAdapter.Connection = con; 
            this.jurnaleTableAdapter.Fill(this.eSRPDataSet.Jurnale,ModuleInfo.CodUnitate);
            this.balconSetupTableAdapter.Fill(eSRPDataSet.BalconSetup, ModuleInfo.CodUnitate);
            this.sectorBugetarTableAdapter.Connection = con;
            this.sectorBugetarTableAdapter.Fill(this.eSRPDataSet.SectorBugetar);
            this.surseFinantareTableAdapter.Connection = con;
            this.surseFinantareTableAdapter.Fill(this.eSRPDataSet.SurseFinantare);
            
            this.rulajeTableAdapter.Fill(this.eSRPDataSet.Rulaje, ModuleInfo.CodUnitate);
            if (filtruNote != String.Empty)
            {
                this.rulajeDocTableAdapter.FillByCont(this.eSRPDataSet.RulajeDoc, ModuleInfo.CodUnitate, filtruNote);
                Text += " contin " + filtruNote;
            }
            else
            {
                if (listBndSourceMain[0] != null && listBndSourceMain[0].Count==0)
                    this.rulajeDocTableAdapter.FillByMonth(this.eSRPDataSet.RulajeDoc, ModuleInfo.CodUnitate, Convert.ToDecimal(LunaCurenta));
                
            }


           
            
            ReadOnly = true;
            if (balconSetupBindingSource.Current != null)
            {
                BSetupRow = ((DataRowView)balconSetupBindingSource.Current).Row as ESRPDataSet.BalconSetupRow;
            }
            InitGrid();
            #region Setare latime coloane conturi in functie de cel mai lung cont
            SqlCommand com = new SqlCommand(String.Empty, con);
            com.CommandText = "select MAX(LEN(ltrim(RTRIM(Cont)))) from Conturi where CodUnitate=" + ModuleInfo.CodUnitate.ToString();
            Object rId = com.ExecuteScalar();
            if (rId != null && rId != DBNull.Value)
            {
                ContCredit.Width = Convert.ToInt32(rId) * 8;
                ContDebit.Width = ContCredit.Width;
            }

            #endregion
            InchideConexiune(con);
            ActiveControl = rezNoteDGV;
            HandleSalvare += new EventHandler(Selectie);
            
            

        }
        public void Tipareste(object sender, EventArgs e)
        {
            String numar, data,capitol,tip;
            


            DataGridViewColumn dc = this.rezNoteDGV.Columns["IDDocumentColumn"];
            DataGridViewColumn dc1 = this.rezNoteDGV.Columns["NumarDocColumn"];
            DataGridViewColumn dc2 = this.rezNoteDGV.Columns["DataDocColumn"];
            DataGridViewColumn dc3 = this.rezNoteDGV.Columns["NumarNotaColumn"];
            DataGridViewColumn dc4 = this.rezNoteDGV.Columns["TipColumn"];

            if (dc == null || dc1==null || dc2==null)
            {
                MessageBox.Show("IDDocument - coloana inexistenta","Eroare",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                return;
            }
            DataGridViewRow curRow = this.rezNoteDGV.CurrentRow;
            numar = curRow.Cells[dc1.Index].EditedFormattedValue.ToString();
            data = curRow.Cells[dc2.Index].EditedFormattedValue.ToString();
            tip = curRow.Cells[dc4.Index].Value.ToString();
            capitol = curRow.Cells[dc3.Index].EditedFormattedValue.ToString();
            try
            {
                this.rulajeTableAdapter.Connection = DeschideConexiune("ESRPConnectionString");
                this.rulajeTableAdapter.GetNota(this.eSRPDataSet.Rulaje, ((long)(System.Convert.ChangeType(curRow.Cells[dc.Index].EditedFormattedValue, typeof(long)))), ModuleInfo.CodUnitate);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                InchideConexiune(this.rulajeTableAdapter.Connection);
            }


            PreviewRaportFrm frm = new PreviewRaportFrm("NotaContabilaRep.rdlc",null,ModuleInfo);
            frm.AddDataSource("ESRPDataSet_Rulaje", this.rulajeBindingSource);
            frm.AddDataSource("ESRPDataSet_BalconSetup", this.balconSetupBindingSource);
            frm.AddParameter("Nr", numar, true);
            frm.AddParameter("Data", data, true);
            frm.AddParameter("Capitol", capitol, true);
            frm.AddParameter("Tip", tip, true);

            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void NoteContabileRezumatFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            gridRezumat = null;
            rezNoteDGV.Dispose();
        }
        override public void SetCurentMonth(short vCodUnitate)
        {
           // Setup(conn);
        }
        /// <summary>
        /// cand se apasa enter, controlul activ este luna, anul
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        override public void toolStripLabelAnLuna_Click(object sender, EventArgs e)
        {

        }
        
    }
}
